﻿using AutoMapper;
using PagedList;
using SIG.Infrastructure.Configs;
using SIG.Model.Videos;
using SIG.Service;
using SIG.ViewModel.Front.Videos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    public class VideoController : Controller
    {
        private readonly IVideoService _videoService;
        private readonly IPageMetaService _pageMetaService;
        private readonly IMapper _mapper;

        public VideoController(IVideoService videoService, IPageMetaService pageMetaService, IMapper mapper)
        {
            _videoService = videoService;
            _pageMetaService = pageMetaService;
            _mapper = mapper;

        }
        // GET: Video

        public ActionResult Index(int? page, string keyword)
        {
            var videoListVM = new VideoListVM();

            videoListVM.Keyword = keyword;
            videoListVM.PageIndex = (page ?? 1) - 1;
            videoListVM.PageSize = 10;
            int totalCount;
            var goodslist = _videoService.GetPagedActiveElements(videoListVM.PageIndex, videoListVM.PageSize, videoListVM.Keyword, out totalCount);
            var categoryVMList = _mapper.Map<List<Video>, List<VideoVM>>(goodslist);
            videoListVM.TotalCount = totalCount;
            videoListVM.Videos = new StaticPagedList<VideoVM>(categoryVMList, videoListVM.PageIndex + 1, videoListVM.PageSize, videoListVM.TotalCount); ;

            return View(videoListVM);

        }


        public ActionResult List(int? page)
        {
            var annListVM = new VideoListVM();

            annListVM.PageIndex = (page ?? 1) - 1;
            annListVM.PageSize = SettingsManager.Video.PageSize;
            int totalCount;
            var list = _videoService.GetPagedActiveElements(annListVM.PageIndex, annListVM.PageSize, string.Empty, out totalCount).ToList();
            var listVM = _mapper.Map<List<Video>, List<VideoVM>>(list);
            annListVM.TotalCount = totalCount;

            annListVM.Videos = new StaticPagedList<VideoVM>(listVM, annListVM.PageIndex + 1, annListVM.PageSize, annListVM.TotalCount); ;

          
            return View(annListVM);

        }
        public ActionResult Detail(int id)
        {
            var video = _videoService.GetById(id);
            video.ViewCount++;
            _videoService.Update(video);
            return View(video);
        }

        public PartialViewResult LatesterVideos(int count)
        {

            var annlist = _videoService.LatesterVideos(count).ToList();
            var vm = _mapper.Map<List<Video>, List<VideoVM>>(annlist);

            return PartialView("_LatesterVideos", vm);
        }


    }
}